import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'controllers/home_controller.dart';
import 'widgets/sidebar.dart';
import 'widgets/top_bar.dart';
import 'widgets/statistics_cards.dart';
import 'widgets/quick_access_buttons.dart';
import 'widgets/charts.dart';
import 'widgets/recent_activities.dart';
import 'widgets/stock_reports.dart';
import 'widgets/notification_dialog.dart';
import '../about/about_screen.dart';
import '../settings/settings_screen.dart';
import '../help/help_screen.dart';
import '../reports_statistics_screen.dart';
import '../inventory_management_screen.dart';
import '../finance_management_screen.dart';
import '../sales/sales_management_screen.dart';
import '../stock_entry/stock_entry_screen.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => HomeController(),
      child: Consumer<HomeController>(
        builder: (context, controller, _) {
          return Scaffold(
            body: Row(
              children: [
                // ئاساسلىق مەزمۇن
                Expanded(
                  child: LayoutBuilder(
                    builder: (context, constraints) {
                      return SizedBox(
                        width: constraints.maxWidth,
                        child: Column(
                          children: [
                            // يۇقىرى قىسمى
                            TopBar(
                              onNotificationsPressed: () {
                                NotificationDialog.show(context);
                              },
                            ),
                            Expanded(
                              child: SingleChildScrollView(
                                child: Column(
                                  children: [
                                    // ستاتىستىكا كارتىلىرى
                                    const StatisticsCards(),
                                    
                                    // تېز كىرىش كۇنۇپكىلىرى
                                    QuickAccessButtons(
                                      onButtonTap: (title) {
                                        controller.setActiveMenuItem(title);
                                        if (title == 'ئامبار تەكشۈرۈش') {
                                          Navigator.push(
                                            context,
                                            MaterialPageRoute(builder: (context) => const InventoryManagementScreen()),
                                          );
                                        }
                                        else if (title == 'مال كىرگۈزۈش') {
                                          Navigator.push(
                                            context,
                                            MaterialPageRoute(builder: (context) => const StockEntryScreen()),
                                          );
                                        }
                                      },
                                    ),
                                    
                                    // دىئاگراممىلار
                                    const Charts(),
                                    
                                    // يېقىنقى پائالىيەتلەر
                                    const RecentActivities(),
                                    
                                    // ئامبار دوكلاتلىرى
                                    const StockReports(),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                
                // يان بالداق
                Sidebar(
                  isMenuExpanded: controller.isMenuExpanded,
                  expandedWidth: controller.expandedWidth,
                  collapsedWidth: controller.collapsedWidth,
                  activeMenuItem: controller.activeMenuItem,
                  showNewProductBadge: controller.showNewProductBadge,
                  showNewSaleBadge: controller.showNewSaleBadge,
                  showNewReportBadge: controller.showNewReportBadge,
                  onMenuItemTap: (menuItem) {
                    controller.setActiveMenuItem(menuItem);
                    
                    // دىتال ھەققىدە كۇنۇپكىسى باسىلغاندا
                    if (menuItem == 'دىتال ھەققىدە') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const AboutScreen()),
                      );
                    }
                    // تەڭشەكلەر كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'تەڭشەكلەر') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const SettingsScreen()),
                      );
                    }
                    // ياردەم كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'ياردەم') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const HelpScreen()),
                      );
                    }
                    // دوكلات ۋە ستاتىستىكا كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'دوكلات ۋە ستاتىستىكا') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const ReportsStatisticsScreen()),
                      );
                    }
                    // ئامبار باشقۇرۇش كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'ئامبار باشقۇرۇش') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const InventoryManagementScreen()),
                      );
                    }
                    // مالىيە باشقۇرۇش كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'مالىيە باشقۇرۇش') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const FinanceManagementScreen()),
                      );
                    }
                    // مال سېتىش كۇنۇپكىسى باسىلغاندا
                    else if (menuItem == 'مال سېتىش') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const SalesManagementScreen()),
                      );
                    }
                    // مال كىرگۈزۈش كۇنۇپكىسى باسىلغاندا (يان تىزىملىك)
                    else if (menuItem == 'مال كىرگۈزۈش') {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => const StockEntryScreen()),
                      );
                    }
                  },
                  onToggleMenu: () {
                    controller.toggleMenu();
                  },
                ),
              ],
            ),
          );
        },
      ),
    );
  }
} 